<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\dao\car;

use app\dao\BaseDao;
use app\model\car\CarStaff;

class CarStaffDao extends BaseDao
{
    public function setModel(): string
    {
        return CarStaff::class;
    }

    /**
     * 列表
     * @param array $where
     * @param string $field
     * @param int $page
     * @param int $limit
     * @return array
     */
    public function getList(array $where=[],string $field = '*',int $page = 0,int $limit = 0 ) {
        $model = $this->getModel()
            ->alias('s')
            ->join('base_place p','s.place_id = p.id','left')
            ->field('s.*,p.title,p.name')
            ->where('s.is_del',0);
        if(!empty($where['place_id'])) {
            $model = $model->where('s.place_id',$where['place_id']);
        }
        if($where['field_key']=="all") {
            $model = $model->where('s.user_phone | s.user_name','like','%'.$where['keywords'].'%');
        }
        if($where['field_key']=="phone") {
            $model = $model->where('s.user_phone','like','%'.$where['keywords'].'%');
        }
        if($where['field_key']=="username") {
            $model = $model->where('s.user_name','like','%'.$where['keywords'].'%');
        }
        $count = $model->order('s.add_time DESC')->count();
        $list = $model->order('s.add_time DESC')->page($page,$limit)->select()->toArray();
        return compact('count','list');
    }

    public function check_login_name($login_name) {
        $model = $this->getModel()->where('login_name',$login_name)->select()->count();
        return $model;
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(){
        return $this->search(['is_del',0])->with(['Has'])->field('*')->select()->toArray();
    }

    /**
     * 删除
     * @param int $id
     * @return array
     */
    public function del (int $id) {
        $del = $this->getModel()->where('id',$id)->useSoftDelete('delete_time',time())->delete();
        $this->getModel()->where('id',$id)->useSoftDelete('is_del',1)->delete();
        return ['删除成功',$del];
    }
}
